package com.iwhalecloud.bss.kite.cucc.service.rule.check.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.*;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalDcPublicConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.service.rule.RuleUtil;
import com.ztesoft.bss.common.bo.StaffInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import com.ztesoft.bss.rul.core.client.dto.common.ContextCustDTO;
import com.ztesoft.bss.rul.core.client.dto.common.RuleCommonParam;
import com.ztesoft.bss.rul.core.client.dto.kite.KiteRuleRequest;
import com.ztesoft.bss.rul.core.client.dto.offer.OfferInst;
import com.ztesoft.bss.rul.core.client.dto.prod.ProdInst;
import com.ztesoft.bss.rul.core.client.dto.rule.InputObject;
import com.ztesoft.bss.rul.core.client.dto.rule.OfferInstComp;
import com.ztesoft.bss.rul.core.client.dto.rule.ProdInstComp;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.*;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.goods.*;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.order.Attachment;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.order.Developer;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.order.LinkMan;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.order.Operaror;
import com.ztesoft.bss.rul.local.dataplug.valid.shopcart.dto.subdto.Attr;
import com.ztesoft.common.logger.api.ZLogger;
import com.ztesoft.common.logger.factory.ZLoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 购物车规则业务对象转换
 * @author kangchuang
 * @date 2021/06/21
 */

public class CuccRuleUtil {
    public static final ZLogger LOGGER = ZLoggerFactory.getLogger(CuccRuleUtil.class, KeyConsts.LOG_MODULE);


    /**
     * Description: <br>
     *
     * @return <br>
     * @taskId <br>
     */
    public static RuleCommonParam getRuleParam(String custId) {
        RuleCommonParam param = new RuleCommonParam();
        param.setLanId(ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr());
        param.setOrgId(ContextUtil.getSimpleLoginInfo().getOrgId().toString());
        param.setSysUserId(ContextUtil.getUserId().toString());
        param.setCustId(custId);
        return param;
    }

    /**
     * 购物车规则相关业务对象转换
     *
     * @param ruleRequest
     * @param busiObject
     * @return
     */
    public static CartGroupInstReq transGroupCardInstToReq(KiteRuleRequest ruleRequest, InputObject busiObject) {
        CartGroupInstReq cartGroupInstReq = new CartGroupInstReq();
        if (!KiteObjectUtils.isEmpty(ruleRequest)) {
            RuleCommonParam ruleCommonParam = ruleRequest.getRuleCommonParam();

            //受理基本信息
            PosBaseInfo posBaseInfo = transForPosBaseInfo(ruleRequest);
            if (!KiteObjectUtils.isEmpty(posBaseInfo)) {
                cartGroupInstReq.setPosBaseInfo(posBaseInfo);
            }
        }
        if (!KiteObjectUtils.isEmpty(busiObject)) {
            //受理业务信息
            List<PosBusiInfo> posBusiInfos = transForPosBusInfo(ruleRequest, busiObject);
            if (!KiteListUtils.isEmpty(posBusiInfos)) {
                cartGroupInstReq.setPosBusiInfos(posBusiInfos);
            }
            //受理相关订单信息
            Order order = transForOrder(ruleRequest, busiObject);
            if (!KiteObjectUtils.isEmpty(order)) {
                cartGroupInstReq.setOrder(order);
            }
        }

        return cartGroupInstReq;
    }

    /**
     * 受理业务信息
     *
     * @param ruleRequest
     * @param busiObject
     * @return
     */
    private static List<PosBusiInfo> transForPosBusInfo(KiteRuleRequest ruleRequest, InputObject busiObject) {
        List<PosBusiInfo> posBusiInfoList = new ArrayList<>();
        RuleCommonParam ruleCommonParam = ruleRequest.getRuleCommonParam();
        Map<String, Object> params = ruleCommonParam.getParams();
        //商品
        KiteSceneInst sceneInst = RuleUtil.getQuerySceneInstService().getSceneInst(ruleRequest.getSceneInstId());
        String rootInstId = sceneInst.getRootInstId();
        KiteOfferInst rootOfferInst = RuleUtil.getQuerySceneInstService().getOfferInst(ruleCommonParam.getCustId(), rootInstId);
        PosBusiInfo posBusiInfo = new PosBusiInfo();
        List<KiteGoodsInfoDTO> goodsInfoDTOs = new ArrayList<>();
        if (!KiteObjectUtils.isEmpty(rootOfferInst)) {
            //销售品
            List<OfferInstComp> offerInstCompList = busiObject.getOfferInstComps();
            if (KiteListUtils.isNotEmpty(offerInstCompList)) {
                for (OfferInstComp offerInstComp : offerInstCompList) {
                    KiteGoodsInfoDTO goodsInfoDTO = new KiteGoodsInfoDTO();
                    List<OfferInst> offerInsts = offerInstComp.getOfferInsts();
                    if (KiteListUtils.isNotEmpty(offerInsts)) {
                        for (OfferInst offerInst : offerInsts) {
                            KiteSingleGoodsDTO dto = new KiteSingleGoodsDTO();
                            //spu
                            dto.setSaleGoodsCode(offerInst.getOfferId());
                            //todo 缺少sku，
//                            dto.setGoodsSku();
                            dto.setGoodsType(offerInst.getActionType());
                            dto.setInstId(offerInst.getOfferInstId());
                            //产品
                            List<ProdInstComp> prodInstCompList = busiObject.getProdInstComps();
                            List<Product> products = new ArrayList<>();
                            if (KiteListUtils.isNotEmpty(prodInstCompList)) {
                                for (ProdInstComp prodInstComp : prodInstCompList) {
                                    List<ProdInst> prodInsts = prodInstComp.getProdInsts();
                                    if (KiteListUtils.isNotEmpty(prodInsts)) {
                                        //主产品
                                        for (ProdInst prodInst : prodInsts) {
                                            Product product = new Product();
                                            product.setActionType(prodInst.getActionType());
                                            Map<String, Object> kiteAttrMap = prodInst.getAttrMap();
                                            product.setAttrs(dealAttrForMap(kiteAttrMap));
                                            product.setInstId(prodInst.getProdInstId());
                                            product.setProductId(prodInst.getProdId());
                                            products.add(product);
                                        }
                                    }

                                }
                            }
                            //资费
                            List<KiteOfferInst> kiteOfferInsts = new ArrayList<>();
                            List<Map<String, Object>> feesMap = (List<Map<String, Object>>) params.get("offerInstFee");
                            for (Map<String, Object> map : feesMap) {
                                KiteOfferInst kiteOfferInst = new KiteOfferInst();
                                kiteOfferInsts.add(KiteMapUtils.mapToBean(map, kiteOfferInst));
                            }
                            if (KiteListUtils.isNotEmpty(kiteOfferInsts)) {
                                for (KiteOfferInst kiteOfferInst : kiteOfferInsts) {
                                    Product product = new Product();
                                    product.setActionType(kiteOfferInst.getActionType().toString());
                                    product.setInstId(kiteOfferInst.getInstId());
                                    product.setProductId(kiteOfferInst.getOfferId());
                                    product.setProductName(kiteOfferInst.getOfferName());
                                    product.setOriginActionType(kiteOfferInst.getOriginalActionType().toString());
                                    //todo 暂时先将资费默认为12
                                    product.setProductType("12");
                                    Map<String, KiteAttr> feeAttrMap = kiteOfferInst.getAttrMap();
                                    product.setAttrs(dealAttrForMapExt(feeAttrMap));
                                    products.add(product);

                                }

                            }

                            dto.setProducts(products);
                            goodsInfoDTO.setSingleGoods(dto);
                        }
                        goodsInfoDTOs.add(goodsInfoDTO);

                    }
                    //合同
                    List<KiteOfferInstContract> kiteOfferInstContractList = new ArrayList<>();
                    List<Map<String, Object>> contractsMap = (List<Map<String, Object>>) params.get("contracts");
                    for (Map<String, Object> map : contractsMap) {
                        KiteOfferInstContract dto = new KiteOfferInstContract();
                        kiteOfferInstContractList.add(KiteMapUtils.mapToBean(map, dto));
                    }

                    List<Contract> contracts = new ArrayList<>();
                    if (KiteListUtils.isNotEmpty(kiteOfferInstContractList)) {
                        for (KiteOfferInstContract kiteOfferInstContract : kiteOfferInstContractList) {
                            Contract contract = new Contract();
                            KiteBeanUtils.copyProperties(kiteOfferInstContract, contract);
                            contracts.add(contract);
                        }
                        goodsInfoDTO.setContracts(contracts);
                    }
//                Map<String, Object> pa= (Map<String, Object>) contactInfosList.get(0);
//                contract.setActionType(KiteMapUtils.getStrValue(pa,"actionType"));
//                contract.setAttachmentName(KiteMapUtils.getStrValue(pa,"attachmentName"));
//                contract.setContractBatchId(KiteMapUtils.getStrValue(pa,"contractBatchId"));
//                contract.setContractName(KiteMapUtils.getStrValue(pa,"contractName"));
//                contract.setContractNo(KiteMapUtils.getStrValue(pa,"contractNo"));
//                contract.setContractUrl(KiteMapUtils.getStrValue(pa,"contractUrl"));
//                contract.setInstId(KiteMapUtils.getStrValue(pa,"instId"));
                    //付费关系
                    List<KiteOfferInstAcct> instAcctList = new ArrayList<>();
                    List<Map<String, Object>> payAcctsMap = (List<Map<String, Object>>) params.get("contracts");
                    for (Map<String, Object> map : payAcctsMap) {
                        KiteOfferInstAcct dto = new KiteOfferInstAcct();
                        instAcctList.add(KiteMapUtils.mapToBean(map, dto));
                    }

                    List<PayAcct> payAccts = new ArrayList<>();
                    if (KiteListUtils.isNotEmpty(instAcctList)) {
                        for (KiteOfferInstAcct kiteOfferInstAcct : instAcctList) {
                            PayAcct payAcct = new PayAcct();
                            KiteBeanUtils.copyProperties(kiteOfferInstAcct, payAcct);
                            payAccts.add(payAcct);
                        }
                        goodsInfoDTO.setPayAccts(payAccts);
                    }
//                Map<String, Object> payAcctMaps= (Map<String, Object>) payAcctsList.get(0);
//                payAcct.setAccountCycle(KiteMapUtils.getStrValue(payAcctMaps,"accountCycle"));
//                payAcct.setAcctId(KiteMapUtils.getStrValue(payAcctMaps,"acctId"));
//                payAcct.setAcctItemId(KiteMapUtils.getStrValue(payAcctMaps,"acctItemId"));
//                payAcct.setAcctName(KiteMapUtils.getStrValue(payAcctMaps,"acctDesc"));
//                payAcct.setExtParam(KiteMapUtils.getMap(payAcctMaps,payAcctMaps.get("attrMap")));
//                payAcct.setPayCycle(KiteMapUtils.getStrValue(payAcctMaps,"payCycle"));
//                payAcct.setPayLimit(KiteMapUtils.getStrValue(payAcctMaps,"payLimit"));
//                payAcct.setPayModeCode(KiteMapUtils.getStrValue(payAcctMaps,"payModeCode"));
//                payAcct.setPayWay(KiteMapUtils.getStrValue(payAcctMaps,"payWay"));
//                payAcct.setCbssAccountCode(KiteMapUtils.getStrValue(payAcctMaps,"cbssAccountCode"));
//                payAcct.setFeeCycle(KiteMapUtils.getStrValue(payAcctMaps,"feeCycle"));
//                payAcct.setStartDate(KiteMapUtils.getStrValue(payAcctMaps,"startDate"));
//                payAcct.setEndDate(KiteMapUtils.getStrValue(payAcctMaps,"endDate"));
//                payAcct.setAccountCycle(KiteMapUtils.getStrValue(payAcctMaps,"accountCycle"));
//                payAcct.setGroupCustId(KiteMapUtils.getStrValue(payAcctMaps,"groupCustId"));
//                payAcct.setIsDefault(KiteMapUtils.getStrValue(payAcctMaps,"isDefault"));
                    //折扣
                    List<KiteOfferInstDiscount> instDiscounts = new ArrayList<>();
                    List<Map<String, Object>> discountMap = (List<Map<String, Object>>) params.get("offerInstDiscount");
                    for (Map<String, Object> map : discountMap) {
                        KiteOfferInstDiscount dto = new KiteOfferInstDiscount();
                        instDiscounts.add(KiteMapUtils.mapToBean(map, dto));
                    }
                    List<Discount> discounts = new ArrayList<>();
                    if (KiteListUtils.isNotEmpty(instDiscounts)) {
                        for (KiteOfferInstDiscount kiteOfferInstDiscount : instDiscounts) {
                            Discount discount = new Discount();
                            KiteBeanUtils.copyProperties(kiteOfferInstDiscount, discount);
                            discounts.add(discount);
                        }
                        goodsInfoDTO.setDiscounts(discounts);
                    }
                    //用户属性
                    List<User> users = new ArrayList<>();
                    Map<String, Object> staffMaps = (Map<String, Object>) ruleCommonParam.getParams().get("staffInfo");
                    User user = new User();
                    user.setUserId(ruleCommonParam.getSysUserId());
                    user.setEparchyCode(KiteMapUtils.getStrValue(staffMaps, "postLanId"));
                    //todo 用户实例Id
//            user.setInstId();
                    user.setProvinceCode(KiteMapUtils.getStrValue(staffMaps, "postProvinceNbr"));
                    users.add(user);
                    goodsInfoDTO.setUsers(users);
                    goodsInfoDTOs.add(goodsInfoDTO);
                }
            }
            posBusiInfo.setGoodsInfo(goodsInfoDTOs);
            posBusiInfoList.add(posBusiInfo);
        }


        return posBusiInfoList;
    }

    //资费属性转换
    private static List<Attr> dealAttrForMapExt(Map<String, KiteAttr> feeAttrMap) {
        List<Attr> attrList = new ArrayList<>();
        if (KiteMapUtils.isNotEmpty(feeAttrMap)) {
            List<KiteAttr> kiteAttrList = new ArrayList<>();
            List<Map> discountMap = (List<Map>) feeAttrMap.get("attrMap");
            for (Map<String, Object> map : discountMap) {
                KiteAttr dto = new KiteAttr();
                kiteAttrList.add(KiteMapUtils.mapToBean(map, dto));
            }
            if (KiteListUtils.isNotEmpty(kiteAttrList)) {
                for (KiteAttr kiteAttr : kiteAttrList) {
                    Attr attr = new Attr();
                    attr.setAttrCode(kiteAttr.getFieldName());
                    attr.setAttrName(kiteAttr.getCname());
                    attr.setAttrValue(kiteAttr.getValue());
                    attr.setAttrValueDesc(kiteAttr.getValueDesc());
                    attrList.add(attr);
                }
            }
        }


        return attrList;
    }

    /**
     * 产品属性转换处理
     *
     * @param kiteAttrMap
     * @return
     */
    private static List<Attr> dealAttrForMap(Map<String, Object> kiteAttrMap) {
        List<Attr> attrList = new ArrayList<>();
        if (KiteMapUtils.isNotEmpty(kiteAttrMap)) {
            Map<String, KiteAttr> attrMap = new HashMap<>();
            Set<String> keySet = kiteAttrMap.keySet();
            for (String key : keySet) {
                attrMap.put(key, (KiteAttr) kiteAttrMap.get(key));
            }
            if (KiteMapUtils.isNotEmpty(attrMap)) {
                attrList = dealAttrForMapExt(attrMap);
            }


        }

        return attrList;

    }

//    Map<String, Object> attrMap = new HashMap<>();
//    Map<String,KiteAttr> oldAttrMap = oldInst.getAttrMap();
//        if (KiteMapUtils.isNotEmpty(oldAttrMap)){
//        Set<String> keySet = oldAttrMap.keySet();
//        for(String key: keySet){
//            attrMap.put(key, oldAttrMap.get(key));
//        }
//    }
//        newInst.setAttrMap(attrMap);


    /**
     * 业务对象订单信息
     *
     * @param busiObject
     * @return
     */
    private static Order transForOrder(KiteRuleRequest ruleRequest, InputObject busiObject) {
        Order order = new Order();
        Map<String, Object> ruleRequestParams = ruleRequest.getParams();
//        order.setDeliverys((List<Delivery>) ruleRequestParams.get("deliverInfos"));
        order.setExtParam(busiObject.getSceneParams().get(ruleRequest.getSceneInstId()));
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        //订单属性
        List<KiteAttr> busiAttrs = Lists.newArrayList();
        List<DcPublic> publics = DcPublicCache.get(LocalDcPublicConsts.BUSI_ATTR_MODULE);
        if (KiteListUtils.isNotEmpty(publics)) {
            publics = publics.stream().filter(pp -> KiteStringUtils.equals(pp.getPcode(), LocalKeyConsts.MODULE_ID_ORDER)).collect(Collectors.toList());
            publics.forEach(ppp -> {
                busiAttrs.addAll(querySceneInstService.listCommonAttrsByModuleId(ruleRequest.getSceneInstId(), ppp.getPcode()));
            });
        }
        List<Attr> attrs = Lists.newArrayList();
        for (KiteAttr kiteAttr : busiAttrs) {
            Attr attr = new Attr();
            attr.setAttrCode(kiteAttr.getFieldName());
            attr.setAttrName(kiteAttr.getCname());
            attr.setAttrOldValue(kiteAttr.getOldValue());
            attr.setAttrValue(kiteAttr.getValue() == null ? "" : kiteAttr.getValue());
            attr.setAttrValueDesc(kiteAttr.getValueDesc());
            attrs.add(attr);
        }
        order.setAttrs(attrs);

        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(ruleRequest.getSceneInstId());
        List<KiteAttr> publicAttrs = querySceneInstService.listCommonAttrsByModuleId(kiteSceneInst.getSceneInstId(), LocalKeyConsts.MODULE_ID_PUBLIC);
        if (KiteListUtils.isNotEmpty(publicAttrs)) {
            //联系人
            KiteAttr linkAttr = publicAttrs.stream().filter(attr -> KiteStringUtils.equals(attr.getFieldName(), "contactInfo")).findFirst().orElse(null);
            //发展人
            KiteAttr developerAttr = publicAttrs.stream().filter(attr -> KiteStringUtils.equals(attr.getFieldName(), "devPersonInfo")).findFirst().orElse(null);
            ///经办人
            KiteAttr operaterAttr = publicAttrs.stream().filter(attr -> KiteStringUtils.equals(attr.getFieldName(), "HandlerCustInfo")).findFirst().orElse(null);

            if (!KiteObjectUtils.isEmpty(linkAttr)) {
                List<KiteAttr> linkAttrs = querySceneInstService.listCommonAttrsByModuleId(kiteSceneInst.getSceneInstId(), LocalKeyConsts.MODULE_ID_CONTACT);
                if (!KiteListUtils.isEmpty(linkAttrs)) {
                    LinkMan linkman = new LinkMan();
                    linkman.setName(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_NAME));
                    linkman.setPhone(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_TEL));
                    linkman.setEmail(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_EMAIL));
                    linkman.setAddress(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_ADDRESS));
                    linkman.setFax(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_FAXNUM));
                    linkman.setFixedPhone(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_FIXEDPHONE));
//                linkman.setPostcode(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_POSTCODE, "未配置【客户联系人邮编】属性"));
//                linkman.setPersonIdType(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_CARDTYPE, "未配置【客户联系人证件类型】属性"));
                    linkman.setRemark(getAttrValue(linkAttrs, LocalKeyConsts.CONTACT_INFO_COMMENTS));
                    order.setLinkMan(linkman);
                }
            }
            if (!KiteObjectUtils.isEmpty(developerAttr)) {
                List<KiteAttr> developerAttrs = querySceneInstService.listCommonAttrsByModuleId(kiteSceneInst.getSceneInstId(), LocalKeyConsts.MODULE_ID_CONTACT);
                if (!KiteListUtils.isEmpty(developerAttrs)) {
                    Developer developer = new Developer();
                    developer.setStaffId(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_ID));
                    developer.setChannelId(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_CHANNELID));
                    developer.setDepartId(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_CHANNELID));
                    developer.setChannelName(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_CHANNELNAME));
                    developer.setStaffeName(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_NAME));
                    developer.setPhone(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_TEL));
//                    developer.setEmail(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_EMAIL));
                    developer.setIsSendSms(getAttrValue(developerAttrs, LocalKeyConsts.DEVPERSON_INFO_SENDMESSAGE));
                    order.setDeveloper(developer);
                }
            }
            if (!KiteObjectUtils.isEmpty(operaterAttr)) {
                List<KiteAttr> operaterAttrs = querySceneInstService.listCommonAttrsByModuleId(kiteSceneInst.getSceneInstId(), LocalKeyConsts.MODULE_ID_OPERATOR);
                Operaror operator = new Operaror();
                operator.setName(getAttrValue(operaterAttrs, LocalKeyConsts.HANDLER_INFO_NAME));
                operator.setAddress(getAttrValue(operaterAttrs, LocalKeyConsts.HANDLER_INFO_ADDRESS));
                operator.setEmail(getAttrValue(operaterAttrs, LocalKeyConsts.HANDLER_INFO_EMAIL));
                operator.setIdType(getAttrValue(operaterAttrs, LocalKeyConsts.HANDLER_INFO_CARDTYPE));
                operator.setSex(getAttrValue(operaterAttrs, LocalKeyConsts.HANDLER_INFO_SEX));
                operator.setPhone(getAttrValue(operaterAttrs, LocalKeyConsts.HANDLER_INFO_TEL));
                order.setOperaror(operator);
            }

        }
        //附件及备注
        List<KiteAttr> commAttrs = querySceneInstService
            .listCommonAttrs(kiteSceneInst.getSceneInstId(), KeyConsts.COMMON_OBJECT_TYPE);
        List<KiteAttr> attachements = commAttrs.stream()
            .filter((attr) -> LocalKeyConsts.COMMON_ATTR_KEY_ATTACHMENT.equals(attr.getFieldName()))
            .collect(Collectors.toList());

        if (!KiteListUtils.isEmpty(attachements)) {
            KiteAttr kiteAttr = attachements.get(0);
            List<Attachment> attachments = new ArrayList<>();

            String jsonStr = kiteAttr.getValue();
            if (!KiteStringUtils.isEmpty(jsonStr)) {
                JSONArray arrs = JSONArray.parseArray(jsonStr);
                for (int i = 0; i < arrs.size(); i++) {
                    JSONObject obj = arrs.getJSONObject(i);
                    Attachment attaDto = new Attachment();
                    attaDto.setUrl(obj.getString("attachmentUrl"));
                    attaDto.setCataGory(obj.getString("fileType").replace("ATTACHMENTS_TYPE_", ""));
                    attaDto.setName(KiteStringUtils.join(obj.getString("fileName"), SymbolConsts.PERIOD, obj.getString("type")));
                    attachments.add(attaDto);
                }
            }
            order.setAttachments(attachments);
        }
        //备注
        List<KiteAttr> commAttrList = commAttrs.stream()
            .filter((attr) -> !LocalKeyConsts.COMMON_ATTR_KEY_ATTACHMENT.equals(attr.getFieldName()))
            .collect(Collectors.toList());
        if (!KiteListUtils.isEmpty(commAttrList)) {
            for (KiteAttr kiteAttr : commAttrList) {
                if ("ORD10022".equals(kiteAttr.getFieldName())) {
                    order.setRemak(kiteAttr.getValue() == null ? "" : kiteAttr.getValue());
                }
            }
        }

        return order;
    }


    /**
     * 业务对象基础信息
     *
     * @param ruleRequest
     * @return
     */
    private static PosBaseInfo transForPosBaseInfo(KiteRuleRequest ruleRequest) {
        PosBaseInfo posBaseInfo = new PosBaseInfo();
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String lanId = ContextUtil.getLoginInfo().getUserInfo().getPostLanId();

        RuleCommonParam ruleCommonParam = ruleRequest.getRuleCommonParam();
        if (!KiteObjectUtils.isEmpty(ruleCommonParam)) {
            //因为都是极简后端受理，暂设置为1002
            posBaseInfo.setAcceptMode("1002");
            posBaseInfo.setProvinceCode(provinceCode);
            posBaseInfo.setEparchyCode(lanId);
            //todo 归属省份区县，暂不确定create by kangchuang
            posBaseInfo.setEparchyArea("");
            posBaseInfo.setCustId(ruleCommonParam.getCustId());
            posBaseInfo.setExtParam(ruleCommonParam.getParams());

            //客户信息
            Customer customerDTO = new Customer();
            ContextCustDTO contextCustDTO = ruleCommonParam.getCustInfo();
            if (!KiteObjectUtils.isEmpty(contextCustDTO)) {
                customerDTO.setCustId(contextCustDTO.getCustId());
                customerDTO.setName(contextCustDTO.getCustName());
                customerDTO.setLocation(contextCustDTO.getRegionId());
            }
            posBaseInfo.setCustomer(customerDTO);
        }


        StaffInfo staffInfo = ContextUtil.getLoginInfo().getStaffInfo();
        posBaseInfo.setStaffInfo(staffInfo);
        posBaseInfo.setServiceAction("complexService");
        //购物车标识
//        posBaseInfo.setUpdateFlag("add");

        return posBaseInfo;
    }

    protected static String getAttrValue(List<KiteAttr> developerAttrs, String fieldName) {
        List<KiteAttr> attrs = developerAttrs.stream().filter((attr) -> fieldName.equals(attr.getFieldName())).collect(Collectors.toList());
        if (KiteListUtils.isEmpty(attrs)) {
            return "";
        }
        KiteAttr attr = attrs.get(0);
        return attr.getValue() != null ? attr.getValue() : "";
    }
}
